package io.adrainty.bolee.insurance.web;

import com.baomidou.mybatisplus.core.metadata.IPage;
import io.adrainty.bolee.framework.commons.basic.ResultDTO;
import io.adrainty.bolee.framework.commons.basic.TreeDTO;
import io.adrainty.bolee.framework.commons.utils.ResultDTOUtil;
import io.adrainty.bolee.insurance.models.entity.CategoryEntity;
import io.adrainty.bolee.insurance.service.ICategoryService;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.tags.Tag;
import jakarta.annotation.Resource;
import org.springframework.web.bind.annotation.*;

import java.util.Arrays;
import java.util.List;

/**
 * <p>CategoryController</p>
 *
 * @author AdRainty
 * @version V1.0.0
 * @description 筛选项管理
 * @since 2025/8/11 11:19:41
 */

@Tag(name = "筛选项管理")
@RestController
@RequestMapping("/category")
public class CategoryController {

    @Resource
    private ICategoryService categoryService;

    @PostMapping("/page/{pageNum}/{pageSize}")
    @Operation(summary = "分页获取保险分类")
    public ResultDTO<IPage<CategoryEntity>> findCategoryPage(@PathVariable("pageNum") Integer pageNum,
                                                               @PathVariable("pageSize") Integer pageSize,
                                                               @RequestBody CategoryEntity searchDTO) {
        IPage<CategoryEntity> pageResult = categoryService.findCategoryPage(pageNum, pageSize, searchDTO);
        return ResultDTOUtil.success(pageResult);
    }

    @PostMapping("/list")
    @Operation(summary = "获取保险分类列表")
    public ResultDTO<List<CategoryEntity>> findCategoryList(@RequestBody CategoryEntity entity) {
        List<CategoryEntity> list = categoryService.findCategoryList(entity);
        return ResultDTOUtil.success(list);
    }

    @PostMapping("/tree")
    @Operation(summary = "获取保险分类树")
    public ResultDTO<TreeDTO> findCategoryTree(@RequestBody CategoryEntity entity) {
        TreeDTO treeDTO = categoryService.categoryTree(entity);
        return ResultDTOUtil.success(treeDTO);
    }

    @PutMapping
    @Operation(summary = "创建保险分类")
    public ResultDTO<CategoryEntity> createCategory(@RequestBody CategoryEntity entity) {
        CategoryEntity result = categoryService.createCategory(entity);
        return ResultDTOUtil.success(result);
    }

    @PatchMapping
    @Operation(summary = "修改保险分类")
    public ResultDTO<Boolean> updateCategory(@RequestBody CategoryEntity entity) {
        Boolean result = categoryService.updateCategory(entity);
        return ResultDTOUtil.success(result);
    }

    @DeleteMapping
    @Operation(summary = "删除保险分类")
    public ResultDTO<Boolean> deleteSafeguard(@RequestBody CategoryEntity entity) {
        Boolean result = categoryService.deleteCategory(Arrays.stream(entity.getCheckedIds()).toList());
        return ResultDTOUtil.success(result);
    }

}
